package edu.gdpu.gc;


import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mazebin
 * @date 2021年 07月16日 16:39:21
 * 演示弱引用
 * 弱引用：
 * 能存活到下一次执行GC之前。下次执行GC无论内存是否足够都会被回收
 */
public class WeakReferenceTest1 {
    private static final int _4MB = 4 * 1024 * 1024;

    public static void main(String[] args) {
        List<WeakReference<byte[]>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            WeakReference<byte[]> ref = new WeakReference<>(new byte[_4MB]);
list.add(ref);
            for (WeakReference<byte[]> weakReference : list) {
                System.out.print(weakReference.get()+" ");
            }
            System.out.println();

        }
        System.out.println("结束循环："+list.size());
    }

}
